package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.common.util.UUIDUtil;
import com.softer.wxzj.entity.BaseBankInfo;
import com.softer.wxzj.mapper.BaseBankInfoMapper;
import com.softer.wxzj.service.BaseBankInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * BaseBuildService class
 *
 * @author zcz
 * @date 2019/1/16
 */
@Service
public class BaseBankInfoServiceImpl implements BaseBankInfoService {
    @Autowired
    BaseBankInfoMapper baseBankInfoMapper;

    /**
     * 新增银行
     *
     * @param baseBankInfo 银行信息
     * @return 新增结果
     */
    @Override
    public CommonResult insert(BaseBankInfo baseBankInfo) {
        if (StringUtils.isBlank(baseBankInfo.getBankName())) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "请填写银行名称");
        }
        //校验该银行是否创建
        String id = baseBankInfoMapper.checkName(baseBankInfo.getBankName());
        if (StringUtils.isNotBlank(id)) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "该银行已经添加");
        }
        baseBankInfo.setCreater(ShiroUserUtil.getUserName());
        baseBankInfo.setModifier(ShiroUserUtil.getUserName());
        baseBankInfo.setId(UUIDUtil.getUUID());
        int num = baseBankInfoMapper.insert(baseBankInfo);
        return CommonResult.ResponseResult(num);
    }

    /**
     * 修改银行
     *
     * @param baseBankInfo 银行详情
     * @return 修改结果
     */
    @Override
    public CommonResult update(BaseBankInfo baseBankInfo) {
        baseBankInfo.setModifier(ShiroUserUtil.getUserName());
        int num = baseBankInfoMapper.updateByPrimaryKey(baseBankInfo);
        return CommonResult.ResponseResult(num);
    }

    @Override
    public CommonResult delete(String id) {
        // 判断退款中是否有使用
        String payId = baseBankInfoMapper.checkRefPay(id);
        if (StringUtils.isNotBlank(payId)) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "退款有使用，禁止删除");
        }
        //判断承办银行中是否有使用
        String bankId = baseBankInfoMapper.checkContractorBank(id);
        if (StringUtils.isNotBlank(bankId)) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "承办银行在用，禁止删除");
        }
        int num = baseBankInfoMapper.delete(id);
        return CommonResult.ResponseResult(num);
    }

    @Override
    public PageInfo<BaseBankInfo> getList(Page page, BaseBankInfo baseBankInfo) {
        return null;
    }

    @Override
    public PageInfo<BaseBankInfo> getList(Page page, BaseBankInfo baseBankInfo, String fuzzy) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseBankInfo> list;
        if (StringUtils.isNotBlank(fuzzy)) {
            list = baseBankInfoMapper.fuzzySearch(fuzzy);
        } else {
            list = baseBankInfoMapper.getList(baseBankInfo);
        }
        PageInfo<BaseBankInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<BaseBankInfo> getListToAddBank() {
        return baseBankInfoMapper.getListToAddBank();
    }

    @Override
    public BaseBankInfo getInfo(String id) {
        return baseBankInfoMapper.getInfo(id);
    }

    @Override
    public List<BaseBankInfo> getAllList() {
        return baseBankInfoMapper.getList();
    }


    /**
     * 查询银行详情
     *
     * @param id 银行ID
     * @return 银行详情
     */
    @Override
    public BaseBankInfo detail(String id) {
        return baseBankInfoMapper.getInfo(id);
    }

}
